package com.syni.mdd.sjb.common.datatreaure.service.impl.databank.saasminapp;

import com.alibaba.fastjson.JSON;
import com.syni.mdd.sjb.common.component.utils.common.utils.*;
import com.syni.mdd.sjb.common.component.utils.feign.service.mdd.commonuser.UserRestService;
import com.syni.mdd.sjb.common.datatreaure.constants.ResultMsg;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.databank.MemberRqVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.saasminapp.business.UpdateBusinessInfoRqVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.saasminapp.business.UpdateShareRqVo;
import com.syni.mdd.sjb.common.datatreaure.entity.vo.rq.saasminapp.config.StoreIdVo;
import com.syni.mdd.sjb.common.datatreaure.feign.SaasStoreCenterClient;
import com.syni.mdd.sjb.common.datatreaure.service.databank.saasminapp.MinBusinessService;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
@Log4j2
public class MinBusinessServiceImpl implements MinBusinessService {
    @Autowired
    private UserRestService userRestService;

    @Autowired
    SaasStoreCenterClient saasStoreCenterClient;

    @Override
    public Map<String, Object> getServiceInfo(MemberRqVo memberRqVo) {
        try{
            String postUrl = AppConfig.getProperty("saas.dxshapp.store.center.service") + "/sys/store/service/getServiceInfo";
            RpcResponseVo rpcResponseVo = userRestService.getMiniTokenByUserId_v2(memberRqVo.getUserId(),memberRqVo.getToken());
            if(rpcResponseVo.isSuccess()){
                String minitoken = rpcResponseVo.getData().toString();
                Map<String, Object> sassHeader = RestTemplateUtils.getSassHeader(minitoken);
                sassHeader.put("storeId", memberRqVo.getStoreId());
                String reponseBody = RestTemplateUtils.postForEntityForJsonByLoadBalanced(postUrl,new HashMap<>(),sassHeader);
                Map<String,Object> resultMap = JSON.parseObject(reponseBody,Map.class);
                return resultMap;
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return  ResultMapHelper.failure(ResultMsg.HAND_FAIL);
    }

    @Override
    public Map<String, Object> updateSharing(UpdateShareRqVo updateShareRqVo) {
        try{
            Map<String,Object> headMap = new HashMap<>();
            headMap.put("storeId",updateShareRqVo.getStoreId());
            String postUrl = AppConfig.getProperty("saas.dxshapp.store.center.service") + "/sys/store/setting/modify/limit/get";
            Map<String,Object> params = BeanUtils.objectToMap(updateShareRqVo);
            Map<String, Object> resultMap = postReqMember(postUrl, updateShareRqVo.getToken(), updateShareRqVo.getUserId(), true,headMap,params);
            return resultMap;
        }catch (Exception e){
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }

//    @Override
//    public Map<String, Object> getStoreById(StoreIdVo storeIdVo) {
//        try{
//            String postUrl = AppConfig.getProperty("saas.dxshapp.store.center.service") + "/sys/store/getStoreById?storeId=" + storeIdVo.getStoreId();
//            Map<String,Object> params = new HashMap<>();
//            params.put("storeId",storeIdVo.getStoreId());
//            Map<String, Object> resultMap = getReqWithHead(postUrl, storeIdVo.getToken(), storeIdVo.getUserId(), true,params,new HashMap<>());
//            if(StringUtils.equals(resultMap.get("code").toString(),"200")){
//                return ResultMapHelper.success(resultMap.get("data"),ResultMsg.HAND_SUCCESS);
//            }else{
//                return ResultMapHelper.failure(resultMap.get("msg").toString());
//            }
//        }catch (Exception e){
//            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
//        }
//    }

    @Override
    public Map<String, Object> getStoreById(StoreIdVo storeIdVo) {
        try {
            RpcResponseVo rpcResponseVo = userRestService.getMiniTokenByUserId_v2(storeIdVo.getUserId(),storeIdVo.getToken());
            log.info("=====rpcResponseVo======" + rpcResponseVo);
            if(rpcResponseVo.isSuccess()) {
                String minitoken = rpcResponseVo.getData().toString();
                Map<String, Object> saasHeader = RestTemplateUtils.getSassHeader(minitoken);
                Map<String, Object> params = new HashMap<>();
                params.put("storeId", storeIdVo.getStoreId());
                saasHeader.putAll(params);
                Map<String,Object> resultMap = saasStoreCenterClient.getStoreById(storeIdVo.getStoreId(), storeIdVo.getStoreId(),
                        saasHeader.get("client_id").toString(), saasHeader.get("client_secret").toString(), saasHeader.get("Authorization").toString());
                if (StringUtils.equals(resultMap.get("code").toString(), "200")) {
                    return ResultMapHelper.success(resultMap.get("data"), ResultMsg.HAND_SUCCESS);
                } else {
                    return ResultMapHelper.failure(resultMap.get("msg").toString());
                }
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
    }

    @Override
    public Map<String, Object> updateBusinessInfo(UpdateBusinessInfoRqVo updateBusinessInfoRqVo) {
        try{
            Map<String,Object> headMap = new HashMap<>();
            headMap.put("storeId",updateBusinessInfoRqVo.getStoreId());
            String postUrl = AppConfig.getProperty("saas.dxshapp.store.center.service") + "/sys/store/update";
            Map<String,Object> params = BeanUtils.objectToMap(updateBusinessInfoRqVo);
            Map<String, Object> resultMap = postReqMember(postUrl, updateBusinessInfoRqVo.getToken(), updateBusinessInfoRqVo.getUserId(), true,headMap,params);
            if(StringUtils.equals(resultMap.get("code").toString(),"200")){
                return ResultMapHelper.success(resultMap.get("data"),ResultMsg.HAND_SUCCESS);
            }else{
                return ResultMapHelper.failure(resultMap.get("msg").toString());
            }
        }catch (Exception e){
            return ResultMapHelper.failure(ResultMsg.HAND_FAIL);
        }
    }

    public Map<String,Object> getReqWithHead(String requestUrl,String token,Integer userId,Boolean isExtra,Map<String,Object> extraMap,Map<String,Object> params){
        Map<String,Object> resultMap = null;
        try {
            RpcResponseVo rpcResponseVo = userRestService.getMiniTokenByUserId_v2(userId,token);
            log.info("=====rpcResponseVo======" + rpcResponseVo);
            if(rpcResponseVo.isSuccess()){
                String minitoken = rpcResponseVo.getData().toString();
                Map<String, Object> saasHeader = RestTemplateUtils.getSassHeader(minitoken);
                if(isExtra){
                    saasHeader.putAll(extraMap);
                }
                String reponseBody = RestTemplateUtils.getForEntityWithHead(requestUrl,params,saasHeader);
                resultMap = JSON.parseObject(reponseBody,Map.class);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return resultMap;
    }


    public Map<String,Object> reqMember(String requestUrl,String token,Integer userId,Map<String,Object> params){
        Map<String,Object> resultMap = null;
        try {
            RpcResponseVo rpcResponseVo = userRestService.getMiniTokenByUserId_v2(userId,token);
            log.info("=====rpcResponseVo======" + rpcResponseVo);
            if(rpcResponseVo.isSuccess()){
                String minitoken = rpcResponseVo.getData().toString();
                String reponseBody = RestTemplateUtils.getForEntityWithHead(requestUrl,params,RestTemplateUtils.getSassHeader(minitoken));
                resultMap = JSON.parseObject(reponseBody,Map.class);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return resultMap;
    }

    public Map<String,Object> postReqMemberWithToken(String requestUrl,String minToken,Integer userId,
                                            Boolean isExtra,Map<String,Object> extraMap,Map<String,Object> params){
        Map<String,Object> resultMap = null;
        try {
            Map<String, Object> saasHeader = RestTemplateUtils.getSassHeader(minToken);
            if(isExtra){
                saasHeader.putAll(extraMap);
            }
            String reponseBody = RestTemplateUtils.postForEntityForJsonByLoadBalanced(requestUrl,params,saasHeader);
            resultMap = JSON.parseObject(reponseBody,Map.class);
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return resultMap;
    }

    public Map<String,Object> postReqMember(String requestUrl,String token,Integer userId,
                                Boolean isExtra,Map<String,Object> extraMap,Map<String,Object> params){
        Map<String,Object> resultMap = null;
        try {
            RpcResponseVo rpcResponseVo = userRestService.getMiniTokenByUserId_v2(userId,token);
            log.info("=====rpcResponseVo======" + rpcResponseVo);
            if(rpcResponseVo.isSuccess()){
                String minitoken = rpcResponseVo.getData().toString();
                Map<String, Object> saasHeader = RestTemplateUtils.getSassHeader(minitoken);
                if(isExtra){
                    saasHeader.putAll(extraMap);
                }
                String reponseBody = RestTemplateUtils.postForEntityForJsonByLoadBalanced(requestUrl,params,saasHeader);
                resultMap = JSON.parseObject(reponseBody,Map.class);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return resultMap;
    }

    public Map<String,Object> postFormReqMember(String requestUrl,String token,Integer userId,
                                Boolean isExtra,Map<String,Object> extraMap,Map<String,Object> params){
        Map<String,Object> resultMap = null;
        try {
            RpcResponseVo rpcResponseVo = userRestService.getMiniTokenByUserId_v2(userId,token);
            log.info("=====rpcResponseVo======" + rpcResponseVo);
            if(rpcResponseVo.isSuccess()){
                String minitoken = rpcResponseVo.getData().toString();
                Map<String, Object> saasHeader = RestTemplateUtils.getSassHeader(minitoken);
                if(isExtra){
                    saasHeader.putAll(extraMap);
                }
                String reponseBody = RestTemplateUtils.postForEntity(RestTemplateUtils.getLoadRestTemplate(),requestUrl,params,saasHeader);
                resultMap = JSON.parseObject(reponseBody,Map.class);
            }
        }catch (Exception e){
            log.error(e.getMessage(),e);
        }
        return resultMap;
    }


}
